const coachModel = require('../models/coaches.js')

class CoachesCtrl {

    async list() {
        let lst = await coachModel.find().sort('id');
        return lst.map(m=>m.toJSON());
    }

    async create(coachData) {
        let coach = new coachModel(coachData);
        const a = await coach.save();
        return a.toJSON();
    }

    async retrieve(id) {
        let coach = await coachModel.findById(id);
        return coach?coach.toJSON(): null;
    }

    async upsert(id, coachData) {
        coachData.id = id;
        let coach = await coachModel.upsert(id, coachData);
        return coach.toJSON();
    }
    async removeById(id) {
        await coachModel.removeById(id);
        return 1;
    }
}

module.exports = new CoachesCtrl();